package Leetcode;

public class Leetcode_251 {
    public static double champagneTower(int poured, int query_row, int query_glass) {
        double[] row = {poured};
        for (int i = 1; i <= query_row; i++) {
            double[] nextRow = new double[i + 1];
            for (int j = 0; j < i; j++) {
                double volume = row[j];
                if (volume > 1) {
                    nextRow[j] += (volume - 1) / 2;
                    nextRow[j + 1] += (volume - 1) / 2;
                }
            }
            row = nextRow;
        }
        return Math.min(1, row[query_glass]);
    }

    public static void main(String[] args) {
        int poured = 100000009, query_row = 33, query_glass = 17;
        System.out.println(champagneTower(poured,query_row,query_glass));
    }
}
